﻿using System;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Data;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Plugins.General.CommandBox {
    /// <summary>
    /// Interaction logic for CommandBoxConfigurator.xaml
    /// </summary>
    public partial class CommandBoxConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = true;

        public CommandBoxConfigurator() {
            InitializeComponent();

            this.cCallString.TextChanged += new TextChangedEventHandler(CallString_TextChanged);
            this.cDefaultCommand.TextChanged += new TextChangedEventHandler(DefaultCommand_TextChanged);
            this.cAlignment.SelectionChanged += new SelectionChangedEventHandler(Alignment_SelectionChanged);
            this.cLeft.TextChanged += new TextChangedEventHandler(Left_TextChanged);
            this.cTop.TextChanged += new TextChangedEventHandler(Top_TextChanged);
            this.cWidth.TextChanged += new TextChangedEventHandler(Width_TextChanged);
            this.cDisplay.SelectionChanged += new SelectionChangedEventHandler(Display_SelectionChanged);
            this.cShowInTaskbar.Click += new RoutedEventHandler(ShowInTaskbar_Click);
        }

        private void CallString_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Command Box", "Call String", this.cCallString.Text);
        }
        private void DefaultCommand_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Command Box", "Default Command", this.cDefaultCommand.Text);
        }
        private void Alignment_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            ComboBoxItem cb = e.AddedItems[0] as ComboBoxItem;
            string alignment = cb.Content.ToString().ToLower();
            this._Config.SetString("Command Box", "Alignment", alignment);
        }
        private void Left_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cLeft.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Command Box", "Left", value);
        }
        private void Top_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cTop.Text, out value)) {
                value = 0;
            }

            this._Config.SetInt("Command Box", "Top", value);
        }
        private void Width_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int value;
            if (!int.TryParse(this.cWidth.Text, out value)) {
                value = 300;
            }
            value = Math.Max(200, value);

            this._Config.SetInt("Command Box", "Width", value);
        }
        private void Display_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetInt("Command Box", "Display", this.cDisplay.DisplayIndex);
        }
        private void ShowInTaskbar_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetBool("Command Box", "Show In Taskbar", cShowInTaskbar.IsChecked.Value);
        }
        
        #region IConfigurator<IniFile>
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            // Command entry
            this.cCallString.Text = this._Config.GetString("Command Box", "Call String", "cmdbox");
            this.cDefaultCommand.Text = this._Config.GetString("Command Box", "Default Command", string.Empty);

            // Positioning
            this.cAlignment.Alignment = this._Config.GetString("Command Box", "Alignment", "centre centre");
            this.cLeft.Text = this._Config.GetInt("Command Box", "Left", 0).ToString();
            this.cTop.Text = this._Config.GetInt("Command Box", "Top", 0).ToString();
            this.cWidth.Text = this._Config.GetInt("Command Box", "Width", 300).ToString();

            // Display
            this.cDisplay.DisplayIndex = this._Config.GetInt("Command Box", "Display", -1);

            if (System.Windows.Forms.Screen.AllScreens.Length <= 1) {
                this.cDisplayLabel.Visibility = Visibility.Collapsed;
                this.cDisplay.Visibility = Visibility.Collapsed;
            }

            // Miscellaneous
            this.cShowInTaskbar.IsChecked = this._Config.GetBool("Command Box", "Show In Taskbar", PEnvironment.IsWindows7OrAbove);            
            
            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
